package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AddUserDto;
import com.zzyl.dto.UpdateUserDto;
import com.zzyl.dto.UserPageQueryDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户分页查询")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<UserListVo>> page(@PathVariable("pageNum") Integer pageNum,
                                                         @PathVariable("pageSize") Integer pageSize,
                                                         @RequestBody UserPageQueryDto dto) {
        PageResponse<UserListVo> page = userService.page(pageNum, pageSize, dto);
        return ResponseResult.success(page);
    }

    @ApiOperation("创建用户")
    @PutMapping
    public ResponseResult add(@RequestBody AddUserDto dto){
        userService.add(dto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("用户修改")
    public ResponseResult updateUser(@RequestBody UpdateUserDto dto) {
        userService.updateUser(dto);
        return ResponseResult.success();
    }

    @DeleteMapping("/remove/{userIds}")
    @ApiOperation("删除用户")
    public ResponseResult deleteUser(@PathVariable("userIds") Long userId) {
        userService.deleteUser(userId);
        return ResponseResult.success();
    }

    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用或禁用用户")
    public ResponseResult enableOrDisableUser(@PathVariable("id") Long userId,@PathVariable("status") String status){
        userService.enableOrDisableUser(userId,status);
        return ResponseResult.success();
    }

    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation("密码重置")
    public ResponseResult resetPassWord(@PathVariable("userId") Long userId){
        userService.resetPassword(userId);
        return ResponseResult.success();
    }
}
